package org.firebug.spring.boot.validator.validator;

import org.firebug.common.core.tools.IdCardTools;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 身份证号验证
 * @author 张晓东
 */
public class IdCardValidator implements ConstraintValidator<IdCard, String> {
    private boolean required;

    @Override
    public void initialize(IdCard idCard) {
        required = idCard.required();
    }

    @Override
    public boolean isValid(String idCard, ConstraintValidatorContext context) {
        return required ? IdCardTools.validate(idCard) : StringUtils.isEmpty(idCard);
    }
}